package akayblog;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

public class BlogService {

	public BlogService() {

	}

	public int addEntry(Entry entry) {
		int id = -1;
		String query;
		PreparedStatement preparedStatement;

		Format formater;

		String date;

		formater = new SimpleDateFormat("yyyy.MM.dd HH.mm.ss");

		date = formater.format(new Date());

		Connection connection = getConnection();

		query = "INSERT INTO MAIN_TEXT (TITLE,TEXT,ISSUE_DATE)"
				+ " VALUES(?,?,'" + date + "')";

		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
			preparedStatement.setString(1, entry.getTitle());
			preparedStatement.setString(2, entry.getText());

			preparedStatement.execute();

			ResultSet rs = preparedStatement.getGeneratedKeys();
			if (rs.next()) {
				id = rs.getInt(1);
			}

		} catch (SQLException e) {

			e.printStackTrace();

		}
		updateEntry(entry);
		closeConnection(connection);
		return id;

	}

	public void deleteEntry(Entry entry) {
		String query;
		Statement statement;
		Connection connection = getConnection();

		query = "DELETE FROM MAIN_TEXT WHERE ID =" + entry.getId();
		try {

			statement = (Statement) connection.createStatement();
			statement.execute(query);
		} catch (SQLException e) {

			e.printStackTrace();
		}
		closeConnection(connection);
	}

	public void updateEntry(Entry entry) {

		String query;
		Format formater;
		formater = new SimpleDateFormat("yyyy.MM.dd HH.mm.ss");
		String date;
		date = formater.format(new Date());

		PreparedStatement preparedStatement;
		Connection connection = getConnection();
		query = "UPDATE MAIN_TEXT SET TITLE= ? , TEXT= ? , UPDATE_DATE= '"
				+ date + "' where ID= ?";

		try {

			preparedStatement = (PreparedStatement) connection
					.prepareStatement(query);
			preparedStatement.setString(1, entry.getTitle());
			preparedStatement.setString(2, entry.getText());
			preparedStatement.setInt(3, entry.getId());
			preparedStatement.executeUpdate();

		} catch (SQLException e) {

			e.printStackTrace();
		}
		closeConnection(connection);
	}

	public List<Entry> getEntryList() {

		ArrayList<Entry> list;
		Entry entry;
		String query;
		Statement statement = null;
		Connection connection = getConnection();
		ResultSet resultSet;

		list = new ArrayList<Entry>();
		query = "SELECT * FROM MAIN_TEXT";

		try {
			statement = (Statement) connection.createStatement();
			resultSet = statement.executeQuery(query);

			while (resultSet.next()) {
				entry = new Entry();
				entry.setId(resultSet.getInt("ID"));
				entry.setTitle(resultSet.getString("TITLE"));
				entry.setText(resultSet.getString("TEXT"));
				entry.setIssueDate(resultSet.getTimestamp("ISSUE_DATE"));
				entry.setUpdateDate(resultSet.getDate("UPDATE_DATE"));
				list.add(entry);
			}
		} catch (SQLException e) {
			System.err.println("veri tabanından data alınamadı");
			e.printStackTrace();

		}
		closeConnection(connection);
		return list;

	}

	private Connection getConnection() {
		Connection connection = null;
		try {
			connection = (Connection) DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/mysql", "root", "31dd282");
			connection.setAutoCommit(true);

		} catch (SQLException e) {
			System.err.println("veri tabanına bağlanılamadı");
			e.printStackTrace();
		}
		return connection;
	}

	private void closeConnection(Connection connection) {
		try {
			connection.close();
		} catch (SQLException e) {
			System.err.println("veri tabanı kapatılamadı");
			e.printStackTrace();
		}
	}

}
